package com.lottery.ruok.lottrey.chat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.LoginActivity;
import com.lottery.ruok.lottrey.chat.model.ConversionAdapterEx;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.eventbusObject.RongConversationType;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.rong.imkit.RongContext;
import io.rong.imkit.fragment.ConversationFragment;
import io.rong.imkit.model.ConversationInfo;
import io.rong.imkit.widget.adapter.MessageListAdapter;
import utils.Utils;

public class MyConversationFragment extends ConversationFragment {
    private int type = 0;//1是私聊，2是聊天室
    private int privateChatTime = 0;
    private int publicChatTime = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        super.onCreate(savedInstanceState);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RongConversationType event) {
        if (event.getType() == 1) {
            type = 1;
        } else if (event.getType() == 2) {
            type = 2;
        }
    }

    @Override
    public void onSendToggleClick(View v, String text) {
        if (UserInfo.getToken().equals("")) {
            utils.Utils.showToast("请您先登录");
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
            return;
        }
        if (UserInfo.getChatroom_role() != 2 && UserInfo.getChatroom_role() != 3) {

            if (type == 1) {
                if (privateChatTime != 0) {
                    int currentTima = (int) (System.currentTimeMillis() / 1000);
                    int timeDuring = currentTima - privateChatTime;
                    if (timeDuring < 2) {
                        Toast.makeText(getContext(),"您说话太快了",Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        privateChatTime = currentTima;
                    }
                } else {
                    privateChatTime = (int) (System.currentTimeMillis() / 1000);
                }
            } else if (type == 2) {
                if (publicChatTime != 0) {
                    int currentTima = (int) (System.currentTimeMillis() / 1000);
                    int timeDuring = currentTima - publicChatTime;
                    if (timeDuring < 3) {
                        Toast.makeText(getContext(),"您说话太快了",Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        publicChatTime = currentTima;
                    }
                } else {
                    publicChatTime = (int) (System.currentTimeMillis() / 1000);
                }

            }


            if (text.length() > 200) {
                return;
            }
            if(Common.fobid_words!=null){
                for (String fobid_word : Common.fobid_words) {
                    if (fobid_word.equals(text)) {
                        Utils.showToast(getActivity().getResources().getString(R.string.sensitive_words));
                        return;
                    }
                }
            }
        }
        super.onSendToggleClick(v, text);
    }

    @Override
    public MessageListAdapter onResolveAdapter(Context context) {
        //return super.onResolveAdapter(context);
        return new ConversionAdapterEx(context);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

   /* @Override
    public void onResume() {
        super.onResume();
        RongContext.getInstance().registerConversationInfo(ConversationInfo.obtain(getConversationType(), getTargetId()));
    }
//处理从聊天室跳到单聊有声音
    @Override
    public void onStop() {
        super.onStop();
        RongContext.getInstance().unregisterConversationInfo(ConversationInfo.obtain(getConversationType(), getTargetId()));
    }*/
}
